package com.lxmes.hr.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lxmes.common.core.domain.entity.SysRole;
import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.hr.domain.NcLeaveApprovalRules;
import com.lxmes.hr.mapper.NcLeaveApprovalRulesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.hr.mapper.NcLeaveApprovalMapper;
import com.lxmes.hr.domain.NcLeaveApproval;
import com.lxmes.hr.service.INcLeaveApprovalService;
import javax.validation.Validator;

/**
 * 请假审批单Service业务层处理
 *
 * @author lx
 * @date 2023-04-11
 */
@Service
public class NcLeaveApprovalServiceImpl implements INcLeaveApprovalService
{
    @Autowired
    private NcLeaveApprovalMapper ncLeaveApprovalMapper;

    @Autowired
    private NcLeaveApprovalRulesMapper ncLeaveApprovalRulesMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询请假审批单
     *
     * @param id 请假审批单主键
     * @return 请假审批单
     */
    @Override
    public NcLeaveApproval selectNcLeaveApprovalById(Long id)
    {
        return ncLeaveApprovalMapper.selectNcLeaveApprovalById(id);
    }

    /**
     * 查询请假审批单列表
     *
     * @param ncLeaveApproval 请假审批单
     * @return 请假审批单
     */
    @Override
    public List<NcLeaveApproval> selectNcLeaveApprovalList(NcLeaveApproval ncLeaveApproval)
    {
        List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
        String roleKey = roles.get(0).getRoleKey();
        ncLeaveApproval.setNextRoleName(roleKey);
        ncLeaveApproval.setCreateBy(SecurityUtils.getUsername());
        return ncLeaveApprovalMapper.selectNcLeaveApprovalList(ncLeaveApproval);
    }

    /**
     * 新增请假审批单
     *
     * @param ncLeaveApproval 请假审批单
     * @return 结果
     */
    @Override
    public int insertNcLeaveApproval(NcLeaveApproval ncLeaveApproval)
    {
        ncLeaveApproval.setCreateTime(DateUtils.getNowDate());
        ncLeaveApproval.setCreateBy(SecurityUtils.getUsername());
        return ncLeaveApprovalMapper.insertNcLeaveApproval(ncLeaveApproval);
    }

    /**
     * 修改请假审批单
     *
     * @param ncLeaveApproval 请假审批单
     * @return 结果
     */
    @Override
    public int updateNcLeaveApproval(NcLeaveApproval ncLeaveApproval)
    {
        ncLeaveApproval.setUpdateTime(DateUtils.getNowDate());
        return ncLeaveApprovalMapper.updateNcLeaveApproval(ncLeaveApproval);
    }

    /**
     * 批量删除请假审批单
     *
     * @param ids 需要删除的请假审批单主键
     * @return 结果
     */
    @Override
    public int deleteNcLeaveApprovalByIds(Long[] ids)
    {
        return ncLeaveApprovalMapper.deleteNcLeaveApprovalByIds(ids);
    }

    /**
     * 删除请假审批单信息
     *
     * @param id 请假审批单主键
     * @return 结果
     */
    @Override
    public int deleteNcLeaveApprovalById(Long id)
    {
        return ncLeaveApprovalMapper.deleteNcLeaveApprovalById(id);
    }
    @Override
    public String imporIteminfo(List<NcLeaveApproval> ncLeaveApprovalList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(ncLeaveApprovalList) || ncLeaveApprovalList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcLeaveApproval ncLeaveApproval : ncLeaveApprovalList)
        {
            try
            {
                BeanValidators.validateWithException(validator, ncLeaveApproval);
                ncLeaveApproval.setCreateBy(operName);
                ncLeaveApproval.setUpdateBy(operName);
                this.insertNcLeaveApproval(ncLeaveApproval);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public int updateNcLeaveApproval2(NcLeaveApproval ncLeaveApproval) {
        Long id = ncLeaveApproval.getId();
        NcLeaveApproval ncLeaveApprovalEntity = selectNcLeaveApprovalById(id);
        String spResult = ncLeaveApproval.getSpResult();
        String spInfo = ncLeaveApproval.getSpInfo();
        if("bzz".equals(ncLeaveApprovalEntity.getNextRoleName())){
            if("1".equals(spResult)){
                ncLeaveApprovalEntity.setBzzHd("同意");
            }
            if("0".equals(spResult)){
                ncLeaveApprovalEntity.setBzzHd("拒绝");
            }
            if(StringUtils.isNotNull(spInfo)){
                ncLeaveApprovalEntity.setBzzHd(ncLeaveApprovalEntity.getBzzHd()+"("+spInfo+")");
            }

        }
        if("bmjl".equals(ncLeaveApprovalEntity.getNextRoleName())){
            if("1".equals(spResult)){
                ncLeaveApprovalEntity.setBmjlHd("同意");
            }
            if("0".equals(spResult)){
                ncLeaveApprovalEntity.setBmjlHd("拒绝");
            }
            if(StringUtils.isNull(spInfo)){
                ncLeaveApprovalEntity.setBmjlHd(ncLeaveApprovalEntity.getBmjlHd()+"("+spInfo+")");
            }

        }
        if("dsz".equals(ncLeaveApprovalEntity.getNextRoleName())){
            if("1".equals(spResult)){
                ncLeaveApprovalEntity.setDszHd("同意");
            }
            if("0".equals(spResult)){
                ncLeaveApprovalEntity.setDszHd("拒绝");
            }
            if(StringUtils.isNull(spInfo)){
                ncLeaveApprovalEntity.setDszHd(ncLeaveApprovalEntity.getDszHd()+"("+spInfo+")");
            }
        }

        if(!"0".equals(spResult)){
            NcLeaveApprovalRules rule = new NcLeaveApprovalRules();
            rule.setJobNumber(ncLeaveApprovalEntity.getJobNumber());
            List<NcLeaveApprovalRules> ncLeaveApprovalRulesList = ncLeaveApprovalRulesMapper.selectNcLeaveApprovalRulesList(rule);

            NcLeaveApprovalRules ncLeaveApprovalRules = ncLeaveApprovalRulesList.get(0);
            while (true){
                if(ncLeaveApprovalRules.isBzzHd()){
                    if(StringUtils.isNull(ncLeaveApprovalEntity.getBzzHd())){
                        ncLeaveApprovalEntity.setNextUser("bzz");
                        ncLeaveApprovalEntity.setNextRoleName("bzz");
                        break;
                    }
                }

                if(ncLeaveApprovalRules.isFbjlHd()){
                    if(StringUtils.isNull(ncLeaveApprovalEntity.getFbjlHd())) {
                        ncLeaveApprovalEntity.setNextUser("fbjl");
                        ncLeaveApprovalEntity.setNextRoleName("fbjl");
                        break;
                    }
                }

                if(ncLeaveApprovalRules.isBmjlHd()){
                    if(StringUtils.isNull(ncLeaveApprovalEntity.getBmjlHd())) {
                        ncLeaveApprovalEntity.setNextUser("bmjl");
                        ncLeaveApprovalEntity.setNextRoleName("bmjl");
                        break;
                    }
                }

                if(ncLeaveApprovalRules.isZjlHd()){
                    if(StringUtils.isNull(ncLeaveApprovalEntity.getZjlHd())) {
                        ncLeaveApprovalEntity.setNextUser("zjl");
                        ncLeaveApprovalEntity.setNextRoleName("zjl");
                        break;
                    }
                }

                if(ncLeaveApprovalRules.isDszHd()){
                    if(StringUtils.isNull(ncLeaveApprovalEntity.getDszHd())) {
                        ncLeaveApprovalEntity.setNextUser("dsz");
                        ncLeaveApprovalEntity.setNextRoleName("dsz");
                        break;
                    }
                }

                ncLeaveApprovalEntity.setNextUser("");
                ncLeaveApprovalEntity.setNextRoleName("");
                break;
            }
        }else{
            ncLeaveApprovalEntity.setNextUser("");
            ncLeaveApprovalEntity.setNextRoleName("");
        }
        return ncLeaveApprovalMapper.updateNcLeaveApproval(ncLeaveApprovalEntity);
    }
}
